package org.halk.aspect;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * redis 实现
 * @Author halk
 * @Date 2024/9/19 15:46
 **/
//@Component
public class LockRedisServiceImpl implements LockService{
    
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    
    
    @Override
    public Boolean lock(String key, long expireTime) {
        Boolean b = this.stringRedisTemplate.opsForValue().setIfAbsent(key, "1", expireTime, TimeUnit.MILLISECONDS);
        return b != null && b;
    }

    @Override
    public Boolean unLock(String key) {
        Boolean delete = this.stringRedisTemplate.delete(key);
        return  delete != null && delete;
    }
}
